package com.akuma.redmine.util;

import com.akuma.common.util.HttpRequest;
import com.akuma.common.util.JsonUtils;
import com.akuma.redmine.config.Config;
import org.slf4j.helpers.MessageFormatter;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by akuma on 2016/10/12.
 *
 * @author akuma
 */
public class RedMineHttpRequest {
    public static String adminGet(String s, Object... o) throws IOException, URISyntaxException {
        return adminGet(null, s, o);
    }

    public static String adminGet(Map<String, String> params, String s, Object... o) throws IOException, URISyntaxException {
        if (params == null) {
            params = new HashMap<String, String>();
        }
        s = Config.serverUrl + MessageFormatter.arrayFormat(s, o).getMessage();
        params.put("key", Config.adminKey);
        return HttpRequest.get(s, params);
    }

    public static String adminPost(String json, String s, Object... o) throws IOException, URISyntaxException {
        return adminPost(json, null, s, o);
    }

    public static String adminPost(Object jsonObj, String s, Object... o) throws IOException, URISyntaxException {
        return adminPost(JsonUtils.toJSon(jsonObj), null, s, o);
    }

    public static String adminPost(Object jsonObj, Map<String, String> params, String s, Object... o) throws IOException, URISyntaxException {
        return adminPost(JsonUtils.toJSon(jsonObj), params, s, o);
    }

    public static String adminPost(String json, Map<String, String> params, String s, Object... o) throws IOException, URISyntaxException {
        if (params == null) {
            params = new HashMap<String, String>();
        }
        s = Config.serverUrl + MessageFormatter.arrayFormat(s, o).getMessage();
        params.put("key", Config.adminKey);
        return HttpRequest.postJson(json, s, params);
    }
}
